package com.kalix.ksh.proint.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.biz.ILCTestService;
import com.kalix.ksh.proint.api.dao.ILCTestDao;
import com.kalix.ksh.proint.api.model.PostRequestUtil;
import com.kalix.ksh.proint.entities.LCTestBean;
import com.kalix.ksh.proint.entities.LineChartTitleBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LCTestImpl extends GenericBizServiceImpl<ILCTestDao, LCTestBean> implements ILCTestService {
    public LCTestImpl(){super.init(LCTestBean.class.getName());}

    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_POST","config.ksh.portconf");
    public List<LineChartTitleBean> getListTitle(){
        String sql = "select lt from LineChartTitleBean lt";
        List<LineChartTitleBean> list = dao.find(sql);
        return list;
    }

    public String getValue() {
        String sql = "select li from LCTestBean li";
        List<LCTestBean> list = dao.find(sql);
        List<LineChartTitleBean> l = getListTitle();
        Long ms = System.currentTimeMillis();
        Long cahce_time = ms-90000000;
        Long start_absolute = ms-30000000;
        JSONObject jObject =new JSONObject();
        String one = "";
        JSONArray names = new JSONArray();
        JSONArray values = new JSONArray();
        int number = Integer.parseInt(l.get(0).getMax());
        for(int i=0;i<number;i++){
            JSONObject datalist = new JSONObject();
            String uuid = list.get(i).getUuid();
            values.put(list.get(i).getName());
            String strJson = "{\" + \"\\\"metrics\\\":[\" + \"{\" + \"\\\"tags\\\":{\" +\n" +
                    "\"\\\"mo\\\":[\" + \"\\\"Line.domain=\\\\\\\"defaultEngine\\\\\\\",uuid=\\\\\\\"\"+uuid+\"\\\\\\\"\\\"\" +\n" +
                    "\"]\" + \"},\" + \"\\\"name\\\":\\\"ifOctets\\\"\" + \"}\" + \"],\" +\n" +
                    "\"\\\"cache_time\\\":\"+ cache_time +   \",\" + \"\\\"start_absolute\\\":\" + start_absolute + \"}";
            String json = PostRequestUtil.HttpPostWithJson("http://"+wsIp+":"+wsPort+"/api/v1/datapoints/query",strJson);
            JSONObject jsonObject = new JSONObject(json);
            JSONArray js = jsonObject.getJSONArray("queries");
            JSONObject jj = js.getJSONObject(0);
            JSONArray ss = jj.getJSONArray("results");
            JSONObject s = ss.getJSONObject(0);
            JSONArray arr = s.getJSONArray("values");
            int nu = 0;
            if(number>5){
                nu = number;
            }else{
                nu = 5;
            }
            String twoArr [] = new String[nu];
            if(twoArr.length<5){
                for(int o = 0; o<5 ; o++){
                    JSONArray jarr =  arr.getJSONArray(o);
                    twoArr[o] = "0";
                }
                ArrayList listt = new ArrayList();
                String str = "{";
                for(int k = 1; k <= number; k++){
                    listt.removeAll(listt);
                    for(int p = 0; p < k; p++) {
                        listt.add(twoArr[p]);
                    }
                    if (k == number) {
                        str += "'"+ (k-1) + "':" +listt;
                    } else {
                        str += "'" + (k-1) + "':" +listt + "," ;
                    }
                }
                str += "}";
                int num = i+1;
                jObject.put("dataList"+num, str);
                names.put("线"+num);
            }else{
                for (int j = 0; j < 5; j++) {
                    JSONArray jarr =  arr.getJSONArray(j);
                    one = String.valueOf((jarr.getLong(1) / 1024));
                    twoArr[j] = one;
                }
                ArrayList listt = new ArrayList();
                String str = "{";
                for(int k = 1; k <= (number>5?number:5); k++){
                    listt.removeAll(listt);
                    for(int j = 0; j < k; j++) {
                        listt.add(twoArr[j]);
                    }
                    if (k == (number>5?number:5)) {
                        str += "'"+ (k-1) + "':" +listt;
                    } else {
                        str += "'" + (k-1) + "':" +listt + "," ;
                    }
                }
                str += "}";
                int num = i+1;
                jObject.put("dataList"+num, str);
                names.put("线"+num);
            }
        }
        jObject.put("pList",values);

        JSONArray xAxisData = new JSONArray();
        JSONArray titleData = new JSONArray();
        for (int i = 0; i < 5; i++){
            xAxisData.put(i);
            titleData.put(l.get(0).getTitle());
        }
        jObject.put("xAxisData",xAxisData);
        JSONObject yAxis = new JSONObject();
        yAxis.put("name",l.get(0).getYaxisname());
        yAxis.put("max",l.get(0).getMax());
        jObject.put("yAxis",yAxis);
        jObject.put("lValue",names);
        jObject.put("xAxisName",l.get(0).getAxisname());
        jObject.put("titleData",titleData);
        String stri = jObject.toString();
        return stri;
    }

    @Override
    public String getColumnValue() {

        return null;
    }
}


